/**
 * 功能描述 
 */

define(function (require, exports, module) {
	var Grid = require("grid");
	var aw = require("ajaxwrapper");
	var orgAccount = require("../store/orgAccount");
	var store =require("store");
	var activeUser = store.get("user");	
	var Dialog = require("dialog");		
	var Form = require("form");
	var enmu = require("enums");

    var instance = {
        initComponent: function (params,widget) {
        	var grid = new Grid({
        		parentNode:".J-grid",
//        		autoRender : false,
        		
        		model:{   
        			url:"api/organizationsettlement/queryByAccount",
            		params:function(){
            			var subnav = widget.get("subnav");
            			var obj = {
            					"pkOrganization":activeUser.organization.pkOrganization,
            					"status" : subnav.getValue("status"),
                    			"startDate" : subnav.getValue("settlementDate").start,
                    			"endDate" : subnav.getValue("settlementDate").end,  
            					fetchProperties:orgAccount.fetchProperties,
            			};
            			return obj;        				
            		},
        			columns:[{
        				name:"paymentType",
        				label:"款项类型",
        			},
        			{
        				name:"content",
        				label:"款项内容",
        			},
        			{
        				name:"expectedAmount",
        				label:"金额",
        				format:function(val){
							return Number(val).toFixed(2);
						}
        			},
        			{
        				name:"alliInfo",
        				label:"付款单位",
        				format:function(value,data){
       	            	 if(data.paymentType == "预收"){
       	            		 return data.alliInfo;
       	            	 }else{
       	            		 return data.orgInfo;
       	            	 }
       	             }
        			},
        			{
        				name:"orgInfo",
        				label:"收款单位",
        				 format:function(value,data){
        	            	 if(data.paymentType == "预付"){
        	            		 return data.alliInfo;
        	            	 }else{
        	            		 return data.orgInfo;
        	            	 }
        	             }
        			},
        			{
        				name:"realDate",
        				label:"结算日期",
        				format : "date",
        			},
        			{
        				name:"status",
        				label:"状态",
        				format : function(value, row){
      						 if(value == "Pending"){
      							 return "待结算";
      						 }else{
      							 return "已结算";
      						 }
      					 }
        			},{
        				name:"show",
        				label:"操作",
        				format:"button",
        				formatparams:[{
        					id:"edit",        				
							text:"结算",						
        					handler:function(index,data,rowEle){ 

        						if(data.status == "Settled"){
									Dialog.alert({
										title : "提示",
										content : "已经收费完成，无法继续收费！"
									});
									return false
									
								}
        						var remarkForm = new Form({
        							parentNode:".J-dialog-conponent",
        							autoRender:false,
        							model:{
        							  id:"remarkForm",
        							  defaultButton:false,
        							  layout: "2_1",
    								  items: [{
        							            name: "payer",	        							            
        							            validate:"required",
        							            label: "付款单位",
        							            readonly:true,
        							            defaultValue :data.alliInfo,
    								  		},{
    								  			name:"payee",
    								  			validate:"required",
												label:"收款单位",    												
												readonly:true,
												defaultValue :data.orgInfo,
    								  		},{
        							            name: "content",	        							            
        							            validate:"required",
        							            label: "款项内容",
        							            readonly:true,
        							            defaultValue :data.content,
    							            
    								  		},{
    								  			name:"expectedAmount",
												label:"预收/付金额",
												readonly:true,
												defaultValue :(data.expectedAmount).toFixed(2),
    								  		},{
												name:"realAmount",
												validate : ["required"],
												label:"本次结算金额", 
												exValidate: function(value) {
								                	var reg = /^([1-9][\d]{0,7}|0)(\.[\d]{1,2})?$/;
						                            if (!reg.test(value)) {
						                                return "请输入正确金额！";
						                            } else {
						                                return true;
						                            }
						                        }, 
											},{
												name:"paymentMethod",
												label:"收款方式",
												validate:["required"],
												readonly:true,
												defaultValue :data.paymentMethod,
												
												//options:enmu["com.eling.elcms.travel.model.OrganizationSettlement.PaymentMethod"],

											},{
												name:"isInvoice",
												label:"是否开票",
												readonly:true,
												defaultValue :data.isInvoice==false?"否":"是",
									           },{
												name:"invoiceDate",
												label:"开票日期",
												readonly:true,
												format : "date",
												defaultValue :data.invoiceDate==null?"":moment(data.invoiceDate).format("YYYY-MM-DD"),
											},{
												name:"realDate",
												label:"结算日期",
												format : "date",
												defaultValue :moment(new Date()).format("YYYY-MM-DD"),
											},{
												name:"operator",
												label:"经办人",
												defaultValue:activeUser.name
											},{
												name:"description",
												label:"备注",
												type: "textarea",
						                        "break": true,
											},{
						        				name:"pkOrganizationSettlement",
												type:"hidden"
						        			}]
        								}	       			            	
        						});
    							widget.set("remarkForm",remarkForm);
    							Dialog.showComponent(remarkForm,{
    								defaultButton:false,
    								title: "机构结算",
    								buttons :[{
    									id:"save",
    									text:"保存",
     									className:"btn-primary",
    									handler:function(){
    										if(remarkForm.valid()){
    											var FormData = remarkForm.getData();
    											var saveData = {};
    											
    											saveData.pkOrganizationSettlement = data.pkOrganizationSettlement;
    											saveData.content = FormData.content;
    											saveData.realAmount = FormData.realAmount;
    											//saveData.paymentMethod=FormData.paymentMethod;
    											//saveData.isInvoice=FormData.isInvoice;
    											//saveData.invoiceDate=FormData.invoiceDate;
    											saveData.realDate=moment(new Date()).format("YYYY-MM-DD");
    											saveData.description=FormData.description;    											
    											saveData.operator=activeUser.pkUser; 
    											saveData.pkOrganizationr=data.pkOrganization;      							
    											saveData.pkOrganizatione=data.pkAllianceOrg;      							
    											saveData.version=data.version;      							
    											saveData.status="Settled";      							
//    											saveData.chargeDate = moment(new Date()).format("YYYY-MM-DD");
    											aw.saveOrUpdate("api/organizationsettlement/save",aw.customParam(saveData),function(saveData){
    												Dialog.close();
    							          			widget.get("grid").refresh();
    							       			});
    										};
    									}
    								},{
    									id:"close",
    									text:"取消",
    									handler:function(){
    										Dialog.close();
    									}
    								}]
    							});
        					}
        				},{
        					id:"show",        				
							text:"查看",						
        					handler:function(index,data,rowEle){
//        						alert(data.isInvoice)
        						var remarkForm = new Form({
        							parentNode:".J-dialog-conponent",
        							autoRender:false,
        							model:{
        								defaultButton:false,
        								layout: "2_1",
        								  items: [{
      							            name: "payer",	        							            
    							            validate:"required",
    							            label: "付款单位",
    							            readonly:true,
    							            defaultValue :data.orgInfo
							            
								  		},{
								  			name:"payee",
								  			validate:"required",
											label:"收款单位",    												
											readonly:true,
											defaultValue :data.alliInfo,
								  		},{
    							            name: "content",	        							            
    							            validate:"required",
    							            label: "款项内容",
    							            readonly:true,
    							            defaultValue :data.content
							            
								  		},{
								  			name:"expectedAmount",
											label:"预收/付金额",
											readonly:true,
											defaultValue :(data.expectedAmount).toFixed(2),
								  		},{
											name:"realAmount",
											validate : ["required"],
											label:"本次结算金额",
											readonly:true,
											defaultValue :(data.realAmount).toFixed(2),
										},{
											name:"paymentMethod",
											label:"收款方式",
											readonly:true,
											defaultValue :data.paymentMethod,
										},{
											name:"isInvoice",
											label:"是否开票",
											readonly:true,
											defaultValue : data.isInvoice==false?"否":"是",
										},{
											name:"isInvoiceDate",
											label:"开票日期",
											format : "date",
											readonly:true,
											defaultValue :data.invoiceDate==null?"":moment(data.invoiceDate).format("YYYY-MM-DD"),
										},{
											name:"realDate",
											label:"结算日期",
											format : "date",
											readonly:true,
											defaultValue :data.realDate==null?"":moment(data.realDate).format("YYYY-MM-DD"),
										},{
											name:"operator",
											label:"经办人",
											readonly:true,
											defaultValue :activeUser.name,//data.pkOperator.name,
										},{
											name:"description",
											label:"备注",
											readonly:true,
											defaultValue :data.description,
										}]
        								}	       			            	
        						});
    							widget.set("remarkForm",remarkForm);
    							Dialog.showComponent(remarkForm,{
    								defaultButton:false,
    								title: "机构结算",
    								buttons :[{
    									id:"close",
    									text:"取消",
    									handler:function(){
    										Dialog.close();
    									}
    								}]
    							});
        					}
        				}]

        			}]
        			
        		}
        	});  
        	widget.set("grid",grid);
        }
    };
    module.exports = instance;
});
